﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Drawing;

namespace ToolGab.Cleaner.Forms
{
    ///============================================================================================
    /// <summary>
    /// This form is for cleaning invalid shortcuts and empty folders.
    /// </summary>
    ///============================================================================================
    public partial class ShortcutsForm : Form
    {
        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Store the dateTime starting time, to be able to calculate the time elapsed
        /// </summary>
        ///----------------------------------------------------------------------------------------
        private DateTime m_Start;

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Folders deleted count
        /// </summary>
        ///----------------------------------------------------------------------------------------
        private long m_FoldersDeleted = 0;
        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Shortcuts deleted count
        /// </summary>
        ///----------------------------------------------------------------------------------------

        private long m_ShortcutsDeleted = 0;

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Constructor
        /// </summary>
        ///----------------------------------------------------------------------------------------
        public ShortcutsForm()
        {
            InitializeComponent();
            EnableStates();

        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Start the cleaning process
        /// </summary>
        ///----------------------------------------------------------------------------------------
        private void btnStart_Click(object sender, EventArgs e)
        {

            // If the user have checked the deleteFolder option, show a message box to make sure
            // he understand the consequences
            if (chkDeleteFolder.Checked)
            {
                if (MessageBox.Show("You will remove empty folder, are you sure you want to continue?"
                    , Application.ProductName, MessageBoxButtons.YesNo
                    , MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)
                    != DialogResult.Yes)
                    return;
            }

            // Create the directories list that we need to clean up
            List<DirectoryInfo> diList = new List<DirectoryInfo>();
            // If the user have selected the desktop folder
            if (Properties.Settings.Default.ShortcutCleanerDesktop)
                diList.Add(new DirectoryInfo(System.Environment.
                    GetFolderPath(Environment.SpecialFolder.Desktop)));
            // If the user have selected the Start Menu option
            if (Properties.Settings.Default.ShortcutCleanerStartMenu)
                diList.Add(new DirectoryInfo(System.Environment.
                    GetFolderPath(Environment.SpecialFolder.StartMenu)));

            // If we have at least one folder
            if (diList.Count > 0)
            {
                // Disable the start button
                btnStart.Enabled = false;
                pbStatus.Style = ProgressBarStyle.Marquee;

                // Set the start time
                m_Start = DateTime.Now;
                timTime.Start();

                // Empty the items list
                lvwDeleted.Items.Clear();
                m_FoldersDeleted = 0;
                m_ShortcutsDeleted = 0;

                // Set the event handlers
                Shortcuts.RemoveInvalidShortcutCompleted += new EventHandler(Shortcuts_RemoveInvalidShortcutCompleted);
                Shortcuts.RemoveInvalidShortcutProgressChanged += new RmInvSCProgressChangedEventHandler(Shortcuts_RemoveInvalidShortcutProgressChanged);
                Shortcuts.RemoveInvalidShortcutASync(diList);

                // Set the time
                SetInfos();
            }

        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// When the progress change (a folder or shortcut is deleted, we added to the listview
        /// </summary>
        /// <param name="FSI">The source object and the target path</param>
        ///----------------------------------------------------------------------------------------
        private void Shortcuts_RemoveInvalidShortcutProgressChanged(RmInvSCProgressChangedEventArgs FSI)
        {
            ListViewItem lvi = new ListViewItem(FSI.IOObject.FullName);

            // We set up style for the folders
            if (FSI.IOObject is DirectoryInfo)
            {
                lvi.ImageIndex = 0;
                lvi.BackColor = Color.Beige;
                m_FoldersDeleted ++;
            }
            else
            {
                // Everything else, suppose to be only shortcuts(FileInfo)
                lvi.ImageIndex = 1;
                lvi.SubItems.Add(FSI.TargetPath);
                m_ShortcutsDeleted ++;
            }

            // Add this item to the list.
            lvwDeleted.Items.Add(lvi);
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// When all the processes are finished
        /// </summary>
        ///----------------------------------------------------------------------------------------
        private void Shortcuts_RemoveInvalidShortcutCompleted(object sender, EventArgs e)
        {
            pbStatus.Style = ProgressBarStyle.Continuous;
            timTime.Stop();
            btnStart.Enabled = true;
        }
        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Set the information about the cleaning processes
        /// </summary>
        ///----------------------------------------------------------------------------------------
        private void SetInfos()
        {
            lblTimeElapsed.Text = (DateTime.Now - m_Start).ToString().Substring(0, 8); ;
            lblFolderDeleted.Text = m_FoldersDeleted.ToString();
            lblShortcutsDeleted.Text = m_ShortcutsDeleted.ToString();
        }

        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// At each tick, we updates the information
        /// </summary>
        ///----------------------------------------------------------------------------------------
        private void timTime_Tick(object sender, EventArgs e)
        {
            SetInfos();
        }
        ///----------------------------------------------------------------------------------------
        /// <summary>
        /// Cancel the process at the cancel button.
        /// </summary>        
        ///----------------------------------------------------------------------------------------
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Shortcuts.CancelProcess();
        }

        private void chkFolders_CheckedChanged(object sender, EventArgs e)
        {
            EnableStates();
            
        }
        private void EnableStates()
        {
            if (!chkDesktop.Checked && !chkStartMenu.Checked)
                btnStart.Enabled = false;

            else
                btnStart.Enabled = true;
        }
    }
}
